package demo3;

import java.util.Stack;

class MinStack {
    Stack<Integer> stack;
    Stack<Integer> minstack;
    public MinStack() {
        stack =  new Stack<>();
        minstack =  new Stack<>();
    }
    
    public void push(int val) {
        stack.push(val);
        if (minstack.empty()) {
            minstack.push(val);
        } else {
            int top = minstack.peek().intValue();
            if (top >= val) {
                minstack.push(val);
            }
        }
    }
    
    public void pop() {
        if (minstack.peek().equals(stack.peek())) {
            stack.pop();
            minstack.pop();
        } else {
            stack.pop();
        }
    }
    
    public int top() {
        return stack.peek();
    }
    
    public int getMin() {
        return minstack.peek();
    }
}

/**
 * Your MinStack object will be instantiated and called as such:
 * MinStack obj = new MinStack();
 * obj.push(val);
 * obj.pop();
 * int param_3 = obj.top();
 * int param_4 = obj.getMin();
 */